package com.baosight.xCyber;

import com.baosight.ice.util.ConfLoader;
import com.baosight.xCyber.model.TagNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

@Configuration
@PropertySource(value = {"file:${spring.profile.path}/code.properties", "classpath:code.properties"}, ignoreResourceNotFound = true)
public class CodeLoad {

    private Map<String, TagNode> codeMap = new TreeMap<>();

    private Log logger = LogFactory.getLog(this.getClass());

//    @Value("${spring.profile.path}")
//    private String path;


    @PostConstruct
    public void doInit() {
//        Resource resource = null;
//        try {
//            ApplicationHome h = new ApplicationHome(getClass());
//            File jarF = h.getSource();
//            resource = new FileUrlResource(path+"/code.properties");
//
//        } catch (Exception e) {
//            logger.error("未检测到外部目标文件，采用读取内部文件");
//            resource = new ClassPathResource("code.properties");
//            e.printStackTrace();
//        }
//        EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
        Properties properties = ConfLoader.getPropertie("code.properties");


        properties.keySet().forEach(key -> {
            TagNode tagNode = new TagNode();
            try {
                tagNode.tagName = (String) key;
                String temp = properties.getProperty((String) key);
                tagNode.showName = new String(temp.getBytes(), "utf-8").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            codeMap.put((String) key, tagNode);
        });
        System.out.println(codeMap);
        logger.info("code.properties加载完毕");
    }

    public Map<String, TagNode> getCodeMap() {
        return codeMap;
    }

    public void setCodes(Map<String, TagNode> codeMap) {
        this.codeMap = codeMap;
    }
}
